package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cedarsoftware.util.io.JsonWriter;

import dao.DaoUser;
import externaldata.theoricaltimefile.TheoricalTrainData;
import user.User;


public class S_SearchUser extends HttpServlet {
	private static final long serialVersionUID = 1L;


	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	//@SuppressWarnings("unchecked")
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//TODO
		response.setContentType("json");
		PrintWriter out = response.getWriter();
		Map<String, List<TheoricalTrainData>> trains = (Map<String, List<TheoricalTrainData>>) request.getAttribute("paris_disponibles");
		List<TheoricalTrainData> data = new ArrayList<TheoricalTrainData>();
		if(trains != null && !trains.isEmpty()) {
			String station_uic = (String) request.getAttribute("station_uic");
			if("".equals(station_uic)) {
				for(List<TheoricalTrainData> l : trains.values()) {
					data.addAll(l);
				}
			}
			else {
				data = trains.get(station_uic);
			}
			Collections.sort(data);
			out.print(JsonWriter.objectToJson(data));
		}
		else {
			out.print("{}");
		}
		out.close();
	}
}